<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapperName}.base">
	
	<resultMap id="mapper" type="${package}.${name}">
	<#list columns as column>
		<result property="${column.name}" column="${column.columnName}"/>
	</#list>
	</resultMap>
	
	<insert id="insert">
		INSERT INTO ${tableName} (
		<#list columns as column>
			<#if column_index!=0>,</#if>${column.columnName}
		</#list>
		) VALUES (
		<#list columns as column>
			<#if column_index!=0>,</#if>${'#{'}${column.name}${'}'}
		</#list>
	    )
	</insert>
	
	<insert id="insert_dynamic">	
		INSERT INTO ${tableName}
		<trim prefix="(" suffix=")" suffixOverrides=",">
		<#list columns as column>
			<if test="${column.name} != null">${column.columnName},</if>
		</#list>
        </trim>
		<trim prefix=" VALUES (" suffix=")" suffixOverrides=",">
		<#list columns as column>
			<if test="${column.name} != null">${'#{'}${column.name}${'}'},</if>
		</#list>
	    </trim>
	</insert>
	
	<delete id="deleteById">
		DELETE FROM ${tableName}
		<include refid="select_where_id"/>
	</delete>
	
	<update id="update">
		UPDATE ${tableName}
		<set>
		<#list columns as column>
		<#if !(column.isIdColumn??)>
			<#if column_index!=0>,</#if>${column.columnName} = ${'#{'}${column.name}${'}'}
		</#if>
		</#list>
		</set>
		<include refid="select_where_id"/>
    </update>
	
	<update id="update_dynamic">
		UPDATE ${tableName}
		<set>
	        <trim suffixOverrides=",">		
			<#list columns as column>
			<#if !(column.isIdColumn??)>
				<if test="${column.name} != null">${column.columnName} = ${'#{'}${column.name}${'}'},</if>
			</#if>
			</#list>
	        </trim>
		</set>
		<include refid="select_where_id"/>
    </update>
	
	<select id="getById" resultMap="mapper">
		SELECT 
			<include refid="select_columns_"/>
		FROM ${tableName}
		<include refid="select_where_id"/>
	</select>
	
	<select id="list" resultMap="mapper">
		SELECT 
			<include refid="select_columns"/>
		FROM ${tableName}
		<include refid="select_where_claus"/>
	</select>
	
	<sql id="select_columns">
		<choose>
	        <when test="customFields !=null and customFields != ''">
	           	${'${'}customFields${'}'}
	        </when>
	        <otherwise>
				<include refid="select_columns_"/>
	        </otherwise>
	    </choose>
	</sql>
	
	<sql id="select_columns_">
	<#list columns as column>
		<#if column_index!=0>,</#if>${column.columnName}
	</#list>
	</sql>
	
	<sql id="select_where_id">
		<where>
		<#if idColumn??>
			${idColumn.columnName} = ${'#{'}${idColumn.name}${'}'}
		</#if>
		</where>
	</sql>

	<sql id="select_where_claus">
		<where>
	    <#list columns as column>
	    	<#if column.type=='String'>
	    	<if test="${column.name} != null and ${column.name} != ''">AND ${column.columnName} = ${'#{'}${column.name}${'}'}</if>
	    	<if test="${column.name}Like != null and ${column.name}Like != ''">AND ${column.columnName} LIKE ${'#{'}${column.name}Like${'}'}</if>
	    	<#else>
	    	<if test="${column.name} != null">AND ${column.columnName} = ${'#{'}${column.name}${'}'}</if>
	    	<if test="${column.name}Gt != null">AND ${column.columnName} &gt; ${'#{'}${column.name}Gt${'}'}</if>
	    	<if test="${column.name}GtEq != null">AND ${column.columnName} &gt;= ${'#{'}${column.name}GtEq${'}'}</if>
	    	<if test="${column.name}Lt != null">AND ${column.columnName} &lt; ${'#{'}${column.name}Lt${'}'}</if>
	    	<if test="${column.name}LtEq != null">AND ${column.columnName} &lt;= ${'#{'}${column.name}LtEq${'}'}</if>
	    	</#if>
	    	<if test="${column.name}s != null">AND ${column.columnName} IN
	    		<foreach collection="${column.name}s" item="v" index="i" open="(" separator="," close=")">${'#{'}v${'}'}</foreach>
	    	</if>
		</#list>
		</where>
		<choose>
	        <when test="groupBy !=null and groupBy != ''">
	           	GROUP BY ${'${'}groupBy${'}'}
	        </when>
	    </choose>
		<choose>
	        <when test="orderBy !=null and orderBy != ''">
	           	ORDER BY ${'${'}orderBy${'}'}
	        </when>
	        <otherwise>
				ORDER BY CREATE_TIME_ DESC
	        </otherwise>
	    </choose>
	</sql>
</mapper>